﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Business.ProcessingService;
using ESOL.Common;
using ESOL.Common.Entity;
using DevExpress.XtraTreeList.Nodes;

namespace ESOL.WinUI.Sec
{
    public partial class frmMaintainMenu : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public SecMenuEntity MenuEntity { get; set; }
        private SecMenuEntity secMenuParent;
        #endregion

        #region Constructor
        public frmMaintainMenu()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            //cmbMenuType
            List<SecMenuTypeEntity> listMenuType = SecMenuTypeBs.GetMenuTypeListAll();
            SecMenuTypeEntity menuType = new SecMenuTypeEntity();
            listMenuType.Insert(0, menuType);
            cmbMenuType.DataSource = listMenuType;
            cmbMenuType.DisplayMember = "MenuTypeName";
            cmbMenuType.ValueMember = "MenuTypeCode";
        }

        private void SetFields()
        {
            if (MenuEntity == null)
            {
                return;
            }
            foreach (TreeListNode treeListNode in treeList.Nodes)
            {
                if (treeListNode.GetValue("MenuId").ToString().Equals(MenuEntity.ParentMenu))
                {
                    treeList.SetFocusedNode(treeListNode);
                    break;
                }
            }
            cmbMenuType.SetSelectedValue(MenuEntity.MenuTypeCode);
            txtMenuName.Text = MenuEntity.MenuName;
            txtExeFile.Text = MenuEntity.ExeFile;
            txtFormName.Text = MenuEntity.FormName;
            txtMethod.Text = MenuEntity.Method;
            txtOrderIndex.Text = MenuEntity.OrderIndex.ToString();
            txtImageName.Text = MenuEntity.ImageName;
            if (MenuEntity.MasterFlag != null)
            {
                if (MenuEntity.MasterFlag == 1)
                {
                    cbMasterFlag.Checked = true;
                }
                else
                {
                    cbMasterFlag.Checked = false;
                }
            }
        }

        private void BindTreeList()
        {
            List<SecMenuEntity> secMenus=SecMenuBs.GetAllMenus();
            treeList.DataSource = secMenus;
        }

        private void SetEntity()
        {
            if (MenuEntity == null)
            {
                MenuEntity = new SecMenuEntity();
                MenuEntity.MenuId = Guid.NewGuid().ToString();
            }
            MenuEntity.ParentMenu = secMenuParent.MenuId;
            if (MenuEntity.ParentMenu == "") MenuEntity.ParentMenu = null;
            MenuEntity.MenuTypeCode = CommonFunctions.ToString(cmbMenuType.SelectedValue);
            MenuEntity.MenuName = txtMenuName.Text;
            MenuEntity.ExeFile = txtExeFile.Text;
            MenuEntity.FormName = txtFormName.Text;
            MenuEntity.Method = txtMethod.Text;
            MenuEntity.OrderIndex = CommonFunctions.ToInt32(txtOrderIndex.Text);
            MenuEntity.ImageName = txtImageName.Text;
            if (cbMasterFlag.Checked == true)
            {
                MenuEntity.MasterFlag = 1;
            }
            else
            {
                MenuEntity.MasterFlag = 0;
            }
        }

        private bool Save()
        {
            try
            {
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    //bool isSuccessful = SecMenuBs.InsertMenu(MenuEntity);
                    bool isSuccessful = SystemSec.InsertMenu(MenuEntity);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    //bool isSuccessful = SecMenuBs.UpdateMenu(MenuEntity);
                    bool isSuccessful = SystemSec.UpdateMenu(MenuEntity);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void frmMaintainMenu_Load(object sender, EventArgs e)
        {
            BindCombobox();
            BindTreeList();
            SetFields();
        }

        private void treeList_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            secMenuParent = treeList.GetDataRecordByNode(e.Node) as SecMenuEntity;
            txtParentMenu.Text = secMenuParent.MenuName;
            txtParentMenu.Tag = secMenuParent;
        }
        #endregion
    }
}
